﻿Imports System.Xml.Serialization

Public Class PMTGestionArbo
    Implements IXmlSerializable

#Region "Constructeur"
    Public Sub New(Mois As String)
        'InitFromDefaultArbo()
        InitForDesign()
        _Mois = Mois
        'Retraiter chacun des noms en fonction de la valeur de MoisAnnee
    End Sub
    Public Sub New()
        _Mois = "-"
    End Sub
#End Region

#Region "Méthodes"
    Private Sub InitForDesign()
        _NomRacineInfocentre = "C:\TestPMT\Infocentre"
        _NomRacineParametres = "C:\TestPMT\Parametres"

        _NomTbleFluxHistoCRT = "%RacineInfocentre%\%Mois%\FluxHistoCRT\HistoCRT.sas7bdat"
        _NomTbleFluxHistoPMT = "%RacineInfocentre%\%Mois%\FluxHistoPMT\HistoPMT.sas7bdat"
        _NomTbleFluxTxCom = "%RacineInfocentre%\%Mois%\FluxTxCom\FluxTxCom.sas7bdat"
        _NomTbleFluxDerCBP = "%RacineInfocentre%\%Mois%\FluxDerCBP\FluxDerCBP.sas7bdat"
        _NomTbleFluxReass = "%RacineInfocentre%\%Mois%\FluxReass\FluxReass.sas7bdat"
    End Sub
    Private Sub InitFromDefaultArbo()
        'Charger le XML
        Dim DefaultArboconfig As XElement = XElement.Load( _
            IO.Path.Combine(IO.Path.GetDirectoryName(Reflection.Assembly.GetAssembly(Me.GetType()).Location), "DefaultArboConfig.xml"))

        'Créer l'objet arboPMT correspondant
        _NomRacineInfocentre = DefaultArboconfig.<RacineInfocentre>.FirstOrDefault.Value
        _NomRacineParametres = DefaultArboconfig.<RacineParam>.FirstOrDefault.Value

        _NomTbleFluxHistoCRT = DefaultArboconfig.<TbleFluxHistoCRT>.FirstOrDefault.Value
        _NomTbleFluxHistoPMT = DefaultArboconfig.<TbleFluxHistoPMT>.FirstOrDefault.Value
        _NomTbleFluxTxCom = DefaultArboconfig.<TbleFluxTxCom>.FirstOrDefault.Value
        _NomTbleFluxDerCBP = DefaultArboconfig.<TbleFluxDerCBP>.FirstOrDefault.Value
        _NomTbleFluxReass = DefaultArboconfig.<TbleFluxReass>.FirstOrDefault.Value
    End Sub

    Private Function Decomplile_Chaine(chaine As String)
        Dim result As String = chaine
        result = result.Replace("%RacineInfocentre%", NomRacineInfocentre)
        result = result.Replace("%RacineParametres%", NomRacineParametres)
        result = result.Replace("%Mois%", Mois)
        Return result
    End Function
#End Region

#Region "Propriétés"

    Private _Mois As String
    Public Property Mois As String
        Get
            Return _Mois
        End Get
        Set(value As String)
            _Mois = value
            RaiseEvent Mois_Changed(Me, New EventArgs)
        End Set
    End Property

    Private _NomRacineInfocentre As String
    Public ReadOnly Property NomRacineInfocentre As String
        Get
            Return _NomRacineInfocentre.Replace("%Mois%", Mois)
        End Get
    End Property

    Private _NomRacineParametres As String
    Public ReadOnly Property NomRacineParametres As String
        Get
            Return _NomRacineParametres.Replace("%Mois%", Mois)
        End Get
    End Property

    Private _NomTbleFluxHistoCRT As String
    Public ReadOnly Property NomTbleFluxHistoCRT As String
        Get
            Return Decomplile_Chaine(_NomTbleFluxHistoCRT)
        End Get
    End Property

    Private _NomTbleFluxHistoPMT As String
    Public ReadOnly Property NomTbleFluxHistoPMT As String
        Get
            Return Decomplile_Chaine(_NomTbleFluxHistoPMT)
        End Get
    End Property

    Private _NomTbleFluxTxCom As String
    Public ReadOnly Property NomTbleFluxTxCom As String
        Get
            Return Decomplile_Chaine(_NomTbleFluxTxCom)
        End Get
    End Property

    Private _NomTbleFluxDerCBP As String
    Public ReadOnly Property NomTbleFluxDerCBP As String
        Get
            Return Decomplile_Chaine(_NomTbleFluxDerCBP)
        End Get
    End Property

    Private _NomTbleFluxReass As String
    Public ReadOnly Property NomTbleFluxReass As String
        Get
            Return Decomplile_Chaine(_NomTbleFluxReass)
        End Get
    End Property

#End Region

    Public Event Mois_Changed(sender As Object, e As EventArgs)

#Region "Les commandes"

    'Private _CreerDossierCommand As New DelegateCommand(Of String)(AddressOf CreerDossierCanExecute, AddressOf CreerDossierExecute)
    'Public ReadOnly Property OnCreerClick() As DelegateCommand(Of String)
    '    Get
    '        Return _CreerDossierCommand
    '    End Get
    'End Property
    'Private Function CreerDossierCanExecute(param As String) As Boolean
    '    Return True
    'End Function
    'Private Function CreerDossierExecute(param As String) As Boolean
    '    'MsgBox("Une demande de création de dossier a été faite")
    '    'Créer le dossier transmis en paramètre
    '    If Not param Is Nothing Then
    '        Dim DirectoryInfo As New DirectoryInfo(param)
    '        DirectoryInfo.Create()
    '        'Actualiser l'ensemble des propriétés
    '        _RacineParametres.Refresh()
    '        OnPropertyChanged("ExistsRacineParametre")

    '        _RacineInfocentre.Refresh()
    '        OnPropertyChanged("ExistsRacineInfocentre")

    '        _TbleFluxHistoCRT.Refresh()
    '        OnPropertyChanged("ExistsTbleFluxHistoCRT")

    '        _NomTbleFluxHistoPMT.Refresh()
    '        OnPropertyChanged("ExistsTbleFluxHistoPMT")

    '        _TbleFluxTxCom.Refresh()
    '        OnPropertyChanged("ExistsTbleFluxTxCom")

    '        _TbleFluxDerCBP.Refresh()
    '        OnPropertyChanged("ExistsTbleFluxDerCBP")

    '        _NomTbleFluxReass.Refresh()
    '        OnPropertyChanged("ExistsTbleFluxReass")

    '        OnPropertyChanged("AllDirectoriesExist")
    '    End If
    '    Return True
    'End Function

    'Private _ValiderArboCommmand As New DelegateCommand(Of System.Windows.Window)(AddressOf ValiderArboCanExecute, AddressOf ValiderArboExecute)
    'Public ReadOnly Property OnValiderArboClic As DelegateCommand(Of System.Windows.Window)
    '    Get
    '        Return _ValiderArboCommmand
    '    End Get
    'End Property
    'Private Function ValiderArboCanExecute(window As System.Windows.Window) As Boolean
    '    Return True
    'End Function
    'Private Function ValiderArboExecute(window As System.Windows.Window) As Boolean
    '    window.Close()
    '    Return True
    'End Function

#End Region

    Public Function GetSchema() As Xml.Schema.XmlSchema Implements IXmlSerializable.GetSchema
        Return Nothing
    End Function

    Public Sub ReadXml(reader As Xml.XmlReader) Implements IXmlSerializable.ReadXml
        Dim XmlGestionArboSerialization As XElement = XElement.Load(reader)
        _Mois = XmlGestionArboSerialization.<PMTGestionArbo>.<Mois>.FirstOrDefault.Value
        _NomRacineInfocentre = XmlGestionArboSerialization.<PMTGestionArbo>.<NomRacineInfocentre>.FirstOrDefault.Value
        _NomRacineParametres = XmlGestionArboSerialization.<PMTGestionArbo>.<NomRacineParametres>.FirstOrDefault.Value
        _NomTbleFluxHistoCRT = XmlGestionArboSerialization.<PMTGestionArbo>.<NomTbleFluxHistoCRT>.FirstOrDefault.Value
        _NomTbleFluxHistoPMT = XmlGestionArboSerialization.<PMTGestionArbo>.<NomTbleFluxHistoPMT>.FirstOrDefault.Value
        _NomTbleFluxTxCom = XmlGestionArboSerialization.<PMTGestionArbo>.<NomTbleFluxDerCBP>.FirstOrDefault.Value
        _NomTbleFluxDerCBP = XmlGestionArboSerialization.<PMTGestionArbo>.<NomTbleFluxTxCom>.FirstOrDefault.Value
        _NomTbleFluxReass = XmlGestionArboSerialization.<PMTGestionArbo>.<NomTbleFluxReass>.FirstOrDefault.Value
    End Sub

    Public Sub WriteXml(writer As Xml.XmlWriter) Implements IXmlSerializable.WriteXml
        writer.WriteStartElement("PMTGestionArbo")

        writer.WriteElementString("Mois", Mois)
        writer.WriteElementString("NomRacineInfocentre", _NomRacineInfocentre)
        writer.WriteElementString("NomRacineParametres", _NomRacineParametres)
        writer.WriteElementString("NomTbleFluxHistoCRT", _NomTbleFluxHistoCRT)
        writer.WriteElementString("NomTbleFluxHistoPMT", _NomTbleFluxHistoPMT)
        writer.WriteElementString("NomTbleFluxDerCBP", _NomTbleFluxDerCBP)
        writer.WriteElementString("NomTbleFluxTxCom", _NomTbleFluxTxCom)
        writer.WriteElementString("NomTbleFluxReass", _NomTbleFluxReass)

        writer.WriteEndElement()
    End Sub
End Class
